<?php
require("libraries/student.php");
class Video extends Student{
	public function __construct(){
		parent::__construct();
		$this->load->helper("url");
		$this->load->helper("form");
		$this->load->library("string");
		//$this->load->model("model_categories");
		$this->load->model("model_videos");
	}
	public function createThumb($fileName,$path,$width,$height,$islocksize=true){
			$this->load->library('image_lib');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumbs/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = $islocksize;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
	}
	public function index(){
		
		$data["data"]=array();
		$data["template"]="video/index";
		$data["title"]="Video manager";
		$currentparent=$this->input->get("cat")? $this->input->get("cat"):0;
		$data["currentlist"]=$this->model_videos->getdatabycategoryid($currentparent);
		//$data["hierarchydata"]=$this->model_categories->getdata_hierarchy_flat(-1,0,"--");
		$data["hierarchydata"]=array();
		$this->load->view("layout",$data);
	}
	public function add(){
		$data["data"]=array();
		$data["template"]="video/add";
		$data["title"]="Add Post";
		//$data["hierarchydata"]=$this->model_categories->getdata_hierarchy_flat(-1,0,"--");
		$data["hierarchydata"]=array();
		if($this->input->post("ok")){
			$insertdata=array();
			$insertdata["title"]=$this->input->post("title");
			$insertdata["keyword"]=$this->input->post("keyword");
			$insertdata["description"]=$this->input->post("description");
			$insertdata["categoryid"]=$this->input->post("categoryid");
			$insertdata["status"]=$this->input->post("status");
			$insertdata["type"]=$this->input->post("type");
			$insertdata["pathofvideo"]=$this->input->post("pathofvideo");
			$insertdata["contents"]=$this->input->post("contents");
			$insertdata["createdate"]=date("Y-m-d H:m:i");
			$insertdata['avatar']="nofile";
			if($_FILES['avatar']['name'] != NULL){
					$config['upload_path'] = './uploads/video';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1000';
					$config['max_width']  = '750';
					$config['max_height']  = '750';
					$this->load->library('upload',$config);
					if(!is_dir('./uploads/video')){
						mkdir('./uploads/video');
					}
					if(!$this->upload->do_upload("avatar")){
						echo "<script>alert('Add Category Error');</script>";
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$dataupload = $this->upload->data();
						$insertdata['avatar'] = $dataupload['file_name'];
					}
				}
			$this->model_videos->add($insertdata);
			redirect(base_url().'admin/video/index');
		}
		$this->load->view("layout",$data);
	}
	public function update($currentid){
		$data["data"]=array();
		$data["template"]="video/edit";
		$data["title"]="Update Category";
		//$data["hierarchydata"]=$this->model_categories->getdata_hierarchy_flat(-1,0,"--");
		$data["hierarchydata"]=array();
		//$currentid=$this->input->get("id")?$this->input->get("id"):0;
		$insertdata=$this->model_videos->getdata($currentid);
		if(count($insertdata)>0){
			$data["current"]=$insertdata;
			if($this->input->post("ok")){
				$insertdata["title"]=$this->input->post("title");
				$insertdata["keyword"]=$this->input->post("keyword");
				$insertdata["description"]=$this->input->post("description");
				$insertdata["categoryid"]=$this->input->post("categoryid");
				$insertdata["status"]=$this->input->post("status");
				$insertdata["type"]=$this->input->post("type");
				$insertdata["pathofvideo"]=$this->input->post("pathofvideo");
				$insertdata["contents"]=$this->input->post("contents");
				$insertdata["createdate"]=date("Y-m-d H:m:i");
				//$insertdata['avatar']="nofile";
				if($_FILES['avatar']['name'] != NULL){
						$config['upload_path'] = './uploads/video';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '1000';
						$config['max_width']  = '750';
						$config['max_height']  = '750';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("avatar")){
							echo "<script>alert('Add Category Error');</script>";
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$dataupload = $this->upload->data();
							$insertdata['avatar'] = $dataupload['file_name'];
						}
					}
				$this->model_videos->update($insertdata,$currentid);
				redirect(base_url().'admin/video/index');
			}
			
		}else{
			redirect(base_url()."admin/video");
		}
		$this->load->view("layout",$data);
	}
	public function del($id){
		$this->model_videos->del($id);
		redirect(base_url()."admin/video");
	}
}